package com.fenrirtec.aepp.common.api;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.fenrirtec.aepp.common.dto.MemberDto;
import com.fenrirtec.aepp.common.dto.TenderDetailDto;
import com.fenrirtec.aepp.common.service.MemberManageService;
import com.fenrirtec.aepp.common.service.TenderService;
import com.fenrirtec.aepp.common.service.impl.MemberManageServiceImpl;
import com.fenrirtec.aepp.common.service.impl.TenderServiceImpl;
import com.fenrirtec.aepp.common.support.JSONActionSupport;
import com.fenrirtec.core.definition.BaseDefinition.API;
import com.fenrirtec.core.exception.DatabaseException;
import com.fenrirtec.core.exception.UnexpectedException;

public class TenderEnrollAPI extends JSONActionSupport {

	private static final long serialVersionUID = 1L;

	private static final Logger logger = LoggerFactory.getLogger(TenderEnrollAPI.class);

	public TenderEnrollAPI() {
	}
	
	private String param;

	public String getParam() {
		return this.param;
	}

	public void setParam(String param) {
		this.param = param;
	}

    @Override
	protected List<ErrorInfo> doValidate() {
        JSONObject jsonObj = JSONObject.parseObject(this.param);
        Integer tenderId = jsonObj.getInteger("tender_id");
        String memberLoginName = jsonObj.getString("member_login_name");
        List<ErrorInfo> errorList = null;
        MemberManageService memberManageService = new MemberManageServiceImpl();
        TenderService tenderService = new TenderServiceImpl();
        MemberDto memberDto = memberManageService.memberInfo(memberLoginName);
        if (memberDto == null || memberDto.getLastestAuditPhase().intValue() < 9) {
            errorList = new ArrayList<ErrorInfo>();
            ErrorInfo error = new ErrorInfo("您还不是正式会员，不能报名！", ErrorType.ERROR);
            errorList.add(error);
            return errorList;
        }
        TenderDetailDto tenderDetailDto = tenderService.findDetail(tenderId);
        if (tenderDetailDto == null || tenderDetailDto.getDeleteFlag() == true) {
            errorList = new ArrayList<ErrorInfo>();
            ErrorInfo error = new ErrorInfo("抱歉，您要报名的招标信息已被删除！", ErrorType.ERROR);
            errorList.add(error);
            return errorList;
        }
        if (tenderDetailDto.getStatus().intValue() < 2) {
            errorList = new ArrayList<ErrorInfo>();
            ErrorInfo error = new ErrorInfo("抱歉，报名还未开始，现在不能报名！", ErrorType.ERROR);
            errorList.add(error);
            return errorList;
        }
        if (tenderDetailDto.getStatus().intValue() > 2) {
            errorList = new ArrayList<ErrorInfo>();
            ErrorInfo error = new ErrorInfo("抱歉，报名已经截止，现在不能报名！", ErrorType.ERROR);
            errorList.add(error);
            return errorList;
        }
		return null;
	}

	@Override
	protected JSONObject doExecute() {
		
		TenderService service = new TenderServiceImpl();
		Boolean result = false;
		
		try {
			JSONObject jsonObj = JSONObject.parseObject(this.param);
			Integer tenderId = jsonObj.getInteger("tender_id");
			String memberLoginName = jsonObj.getString("member_login_name");
			result = service.enroll(tenderId, memberLoginName);
		} catch (DatabaseException e) {
			if (logger.isErrorEnabled()) {
				logger.error("database error occurred.", e);
			}
			throw new JSONActionStatusException(API.Common.Status.DB_ERROR, e);
		} catch (UnexpectedException e) {
			if (logger.isErrorEnabled()) {
				logger.error("unexpected error occurred.", e);
			}
			throw new JSONActionStatusException(API.Common.Status.DB_ERROR, e);
		}
		
		JSONObject root = new JSONObject();
		root.put("result", result);
		getSession().remove("prePage");
		return root;
	}

}
